package nio;

import io.netty.buffer.ByteBuf;

import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * <b> 描述:</b> ;<br/>
 *
 * @author <b>作者</b>cdxuhongya@jd.com
 *         <b>时间</b>2017/3/1 14:21<br/>
 *         <b>@CopyRight (c) </b>2015-京东智能版权所有<br/>
 */
public class FileChannelTest {
    public static void main(String[] args) throws Exception{
        RandomAccessFile file = new RandomAccessFile("D:\\text.txt","rw");
        FileChannel channel = file.getChannel();
        ByteBuffer buf = ByteBuffer.allocate(30);

        int readBytes =  channel.read(buf);
        while (readBytes != -1){
            System.out.println("read :" + readBytes);
            buf.flip();
            while (buf.hasRemaining()){
                System.out.print((char)buf.get());
            }
            buf.clear();
            readBytes = channel.read(buf);
        }
        file.close();

    }
}
